#coding=utf8
import httplib,urlparse

def getcontent(jsurl,referer):
    "fetch the http content(add header)"
    try:
        if not jsurl and not referer:
            return None
        up = urlparse.urlparse(jsurl)
        con = httplib.HTTPConnection(up.hostname)
        con.putrequest('GET',up.path)
        con.putheader('Referer',referer)
        con.endheaders()
        r = con.getresponse()
        return r.read()
    except Exception, e:
        print e
        return None

def GbkToUtf8(buf):
    try:
        return buf.decode('gbk').encode('utf-8')
    except:
        return buf

def UtfToGbk(buf):
    try:
        return buf.decode('utf-8').encode('gbk')
    except:
        return buf
